package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.core.result.JsonResult;
import com.cjw.campus.core.utils.UploadUtil;
import com.cjw.campus.system.entity.QuestionsTable;
import com.cjw.campus.system.service.IQuestionsTableService;
import com.cjw.campus.system.vo.QuestionsTableVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * <p>
 * 提问表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2023-03-06
 */
@RestController
@RequestMapping("/system/questionsTable")
@Tag(name = "问题管理接口")
public class QuestionsTableController {

    @Autowired
    private IQuestionsTableService iQuestionsTableService;

    @GetMapping("/getByQuestionsId")
    @Operation(summary = "根据id查询问题信息", security = { @SecurityRequirement(name = "Authorization")})
    @Parameter(name = "questionsId")
    public JsonResult getByQuestionsId (Integer questionsId) {
        List<QuestionsTableVO> questionsTable = iQuestionsTableService.getByQuestionsId(questionsId);
        return JsonResult.ok(questionsTable);
    }

    @GetMapping("/listLikeQuestionsTitle")
    @Operation(summary = "根据标题模糊查询问题", security = { @SecurityRequirement(name = "Authorization")})
    @Parameters({
            @Parameter(name = "questionsTitle", description = "问题标题")
    })
    public JsonResult listLikeQuestionsTitle (String questionsTitle) {
        // QueryWrapper MybatisPlus 条件构造器
        QueryWrapper<QuestionsTable> queryWrapper = new QueryWrapper<>();
        // lambda 表达式
        queryWrapper.lambda().like(QuestionsTable::getQuestionsTitle, questionsTitle);
        List<QuestionsTable> mapPage = iQuestionsTableService.list(queryWrapper);
        return JsonResult.ok(mapPage);
    }

    @PostMapping("/save")
    @Operation(summary = "保存问题信息", security = { @SecurityRequirement(name = "Authorization")})
    @Parameter(name = "questionsTable")
//    public JsonResult save (QuestionsTable questionsTable) {
    public JsonResult save (@RequestBody QuestionsTable questionsTable) {
        // 保存问题信息
        boolean save = iQuestionsTableService.save(questionsTable);
        // 判断用户是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @GetMapping("/listDescPageByQuestions")
    @Operation(summary = "倒叙分页查询问题信息", security = { @SecurityRequirement(name = "Authorization")})
    @Parameters({
            @Parameter(name = "currentPage", description = "当前页"),
            @Parameter(name = "pageSize", description = "页数")
    })
    public JsonResult listDescPageByQuestions (Integer currentPage,Integer pageSize) {
        IPage<QuestionsTableVO> page = new Page<>(currentPage, pageSize);
        IPage<QuestionsTableVO> PageQuestions = iQuestionsTableService.listDescPageByQuestions(page);
        return JsonResult.ok(PageQuestions);
    }

}
